package com.chege.seller;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.chege.seller.utils.CommonUtils;
import com.chege.seller.utils.KumaLog;
import com.igexin.sdk.PushManager;

public class MainActivity extends AppCompatActivity {
    private WebView webView;
    private ProgressBar pb;
    private String clientId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initPush();
        initViews();
        initWebView();
    }

    private void initPush() {
        PushManager.getInstance().initialize(this.getApplicationContext());
        clientId = PushManager.getInstance().getClientid(this);
        KumaLog.kuma("MainActivity--clientId:" + clientId);
    }

    private void initViews() {
        webView = (WebView) findViewById(R.id.wv_main);
        pb = (ProgressBar) findViewById(R.id.pb_progress);
    }

    private void initWebView() {
        webView.requestFocus();
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                KumaLog.kuma("onProgressChanged--newProgress:" + newProgress);
                if (newProgress == 100) {
                    pb.setVisibility(View.INVISIBLE);
                } else {
                    if (View.INVISIBLE == pb.getVisibility()) {
                        pb.setVisibility(View.VISIBLE);
                    }
                    pb.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });

        webView.setOnKeyListener(new View.OnKeyListener() {        // webview can go back
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
                    webView.goBack();
                    return true;
                }
                return false;
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Uri uri = Uri.parse(url);
                if (uri.getScheme().equals("tel")) {
                    CommonUtils.callPhone(uri.getHost(), MainActivity.this);
                } else {
                    view.loadUrl(url);   //在当前的webview中跳转到新的url
                }
                return true;
            }


        });

        webView.addJavascriptInterface(new JSInterface(), "jsObj");

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setDefaultTextEncodingName("utf-8");

        webView.loadUrl("http://www.chegecar.com/seller/");
    }

    public class JSInterface {
        @JavascriptInterface
        public String getClientId() {
            return clientId;
        }
    }
}
